﻿using Autofac;
using Common;
using DBContext;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ZoneLine.Models;
using ZoneLine.ViewModels;

namespace ZoneLine.Views
{
    /// <summary>
    /// StationEnableView.xaml 的交互逻辑
    /// </summary>
    public partial class StationEnableView : Window
    {
        public StationEnableView()
        {
            InitializeComponent();
            this.Loaded += StationEnableView_Loaded;
        }

        private bool _canEnter = false;

        private void StationEnableView_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                var config = StationEnableConfig.GetPara();
                if (config == null)
                {
                    DbConfig.DbContext.Insert(new StationEnableConfig()).ExecuteAffrows();
                    config = new StationEnableConfig();
                }

                StationOP006.IsChecked = config.OP006StationEnable == 1;
                StationOP010.IsChecked = config.OP010StationEnable == 1;
                StationOP020.IsChecked = config.OP020StationEnable == 1;
                StationOP050L.IsChecked = config.OP050LStationEnable == 1;
                StationOP050R.IsChecked = config.OP050RStationEnable == 1;
                _canEnter = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("加载系统配置异常" + ex.Message);
                return;
            }
        }

        private void BtnEnable(object sender, RoutedEventArgs e)
        {
            if (_canEnter)
            {
                if (LX.IsChecked == true)
                {
                    Cache.Model = ModelEnum.LX;
                }
                else if (SQ.IsChecked == true)
                {
                    Cache.Model = ModelEnum.SQ;
                }
                else if (BQKH.IsChecked == true)
                {
                    Cache.Model = ModelEnum.BQKH;
                }
                else if (BQLH.IsChecked == true)
                {
                    Cache.Model = ModelEnum.BQLH;
                }
                else if (WL.IsChecked == true)
                {
                    Cache.Model = ModelEnum.WL;
                }
                else
                {
                    MessageBox.Show(this, "必须选择型号后才能进入工作流程");
                    return;
                }

                var workDirection = 0;
                if (EnableL.IsChecked.HasValue && EnableL.IsChecked.Value)
                {
                    workDirection = 1;
                    Cache.WorkDirection = Common.WorkDirectionEnum.Left;
                }
                if (EnableR.IsChecked.HasValue && EnableR.IsChecked.Value)
                {
                    workDirection = 2;
                    Cache.WorkDirection = Common.WorkDirectionEnum.Right;
                }
                var dal = ContainerServer.Container.Resolve<IModelDAL>();
                var saveRlt = dal.UpdateModel<StationEnableConfig>(p => true, s =>
                     new StationEnableConfig()
                     {
                         OP006StationEnable = StationOP006.IsChecked == true ? 1 : 0,
                         OP010StationEnable = StationOP010.IsChecked == true ? 1 : 0,
                         OP020StationEnable = StationOP020.IsChecked == true ? 1 : 0,
                         OP050LStationEnable = StationOP050L.IsChecked == true ? 1 : 0,
                         OP050RStationEnable = StationOP050R.IsChecked == true ? 1 : 0,
                         WorkDirection = workDirection,
                     }
                );
                if (saveRlt)
                {
                    StationEnableConfig.Reload();
                    var view = new MainWindow();
                    view.Show();
                    App.Current.MainWindow = view;
                    this.Close();
                }
            }

        }

        private void EnableL_Checked(object sender, RoutedEventArgs e)
        {

        }
    }

}
